/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.general.bc;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.general.be.TipoUsuario;

/**
 *
 * @author ADMINISTRADOR
 */
@Stateless
public class TipoUsuarioFacade implements TipoUsuarioFacadeLocal {
    @PersistenceContext(unitName = "org.cip.esymons-ejbPU")
    private EntityManager em;

    public void create(TipoUsuario tipoUsuario) {
        em.persist(tipoUsuario);
    }

    public void edit(TipoUsuario tipoUsuario) {
        em.merge(tipoUsuario);
    }

    public void remove(TipoUsuario tipoUsuario) {
        em.remove(em.merge(tipoUsuario));
    }

    public TipoUsuario find(Object id) {
        return em.find(TipoUsuario.class, id);
    }

    public List<TipoUsuario> findAll() {
        return em.createQuery("select object(o) from TipoUsuario as o order by o.tusNombre").getResultList();
    }

    public TipoUsuario buscarTipoDirectivo() {
        try{
        return (TipoUsuario) em.createQuery("select object(o) from TipoUsuario as o where o.tusNombre like 'directivo' ").getSingleResult();
        }catch(Exception ex){
            return null;
        }
    }

    public TipoUsuario buscarTipoColegiado() {
        try{
        return (TipoUsuario) em.createQuery("select object(o) from TipoUsuario as o where o.tusNombre like 'colegiado' ").getSingleResult();
        }catch(Exception ex){
            return null;
        }
    }

    public List<TipoUsuario> findRange(int[] range) {
        Query q = em.createQuery("select object(o) from TipoUsuario as o");
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        return ((Long) em.createQuery("select count(o) from TipoUsuario as o").getSingleResult()).intValue();
    }

}
